package com.lune.blog.gateway.config;


import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yanjizhw@163.com
 * @date 2021/11/1 22:55
 * 描述: 网关流控返回
 * @since 1.0.0
 */
@Configuration
public class SentinelGateWayConfig {

    public SentinelGateWayConfig () {
        GatewayCallbackManager.setBlockHandler((exchange, ex) -> {
            Map<String, String> map = new HashMap<>(3);
            map.put("msg", "请求太频繁");
            map.put("code", "503");
            return ServerResponse.ok().body(Mono.just(map), Map.class);
        });
    }
}
